<!DOCTYPE html>

<html lang="en">
  <head>
    <meta charset="utf-8">
	  <title>Abraham Lincoln's Gettysburg Address</title>

    <link rel="stylesheet" href="style/04.css" type="text/css" />
    <script src="js/jquery.js"></script>
    <script>
      $(function () {
        //css方法，一个参数时获取样式的值，两个参数时候可以设置属性的参数值。
        //获取内容div对象
        let $speech = $("div.speech");
        // //获取最初文字大小
        let defaultSize = $speech.css("font-size");

        $("#switcher button").click(function() {
          //parseFloat会将“12px”转换为“12”
          //$speech.css("font-size") 取得 12px
          let num = parseFloat($speech.css("font-size"));
          switch(this.id){
            case "switcher-large":
              num *= 1.4;
              break;
            case "switcher-small":
              num /= 1.4;
              break;
            default:
              num	= parseFloat(defaultSize);
          }
          $speech.css("font-size",num+"px");
        });

        //需求2
        //eq方法起始下标从0开始，eq(1)则获取第二段落
        /*$("p").eq(1).hide();	//隐藏段落2
        $("a.more").click(function(e) {
            e.preventDefault();	//链接失效
            $("p").eq(1).show();	//段落2显示
            $(this).hide();
        });*/

        //需求3,slow 600毫秒，fast 200毫秒，也可自己指定毫秒数，其他字符400毫秒
        $("p").eq(1).hide();	//隐藏段落2
        $("a.more").click(function(e) {
          e.preventDefault();	//链接失效
          $("p").eq(1).show("slow");	//段落2显示
          $(this).hide();
        });

        //需求4，fadeOut() 淡出
        /*$("p").eq(1).hide();	//隐藏段落2
        $("a.more").click(function(e) {
            e.preventDefault();	//链接失效
            $("p").eq(1).fadeIn("slow");	//段落2显示
            $(this).hide();
        });*/



        //需求5，slideUp()划上效果
        $("p").eq(1).hide();	//隐藏段落2
        $("a.more").click(function(e) {
          e.preventDefault();	//链接失效
          $("p").eq(1).slideDown(2000);	//段落2显示
          $(this).hide();
        });

        //需求6:
        //text(),如果无参数则可以获取div或标签直接的文本内容，
        // 如果有参数，则可以设置中间内容进行替换
       /* var $firstPara = $("p").eq(1);	//得到第二个段落
        $firstPara.hide();			//隐藏它
        $("a.more").click(function(e) {
          e.preventDefault();
          if($firstPara.is(":hidden")){	//如果段落是隐藏的
            $firstPara.fadeIn("slow");	//显示并更换链接文字
            $(this).text("read less");
          }else{
            $firstPara.fadeOut("slow");
            $(this).text("read more");
          }
        });*/

        //需求7
      /*  var $firstPara = $("p").eq(1);	//得到第二个段落
        $firstPara.hide();			//隐藏它
        $("a.more").click(function(e) {
          e.preventDefault();
          //slideToggle通过高度变化来切换
          $firstPara.slideToggle("slow");	//显示隐藏自动切换
          var $like = $(this);
          if($like.text() == "read more"){
            $(this).text("read less");
          }else{
            $(this).text("read more");
          }
        });*/

        //需求8：
        var $firstPara = $("p").eq(1);	//得到第二个段落
        $firstPara.hide();			//隐藏它
        $("a.more").click(function(e) {
          e.preventDefault();
          //animate第一个参数可以传入一个对象来切换属性值，第二个参数可以设置切换的速度
          $firstPara.animate({height:"toggle"},"slow");	//显示隐藏自动切换,height:toggle值表示高度从0变到最大值
          var $like = $(this);
          if($like.text() == "read more"){
            $(this).text("read less");
          }else{
            $(this).text("read more");
          }
        });


       /* $firstPara.animate({height:"toggle",opacity:"toggle"},3000);*/

        //需求10
       /* var $speech = $("div.speech");
        var defaultSize = $speech.css("font-size");
        $("#switcher button").click(function(e) {
          var num = parseFloat($speech.css("font-size"));
          switch(this.id){
            case "switcher-large":
              num *= 1.4;
              break;
            case "switcher-small":
              num /= 1.4;
              break;
            default:
              num	= parseFloat(defaultSize);
          }
          $speech.animate({fontSize:num + "px"},"slow");
        })*/

        //需求11
       /* $("div.label").click(function(e) {
          var paraWidth = $("div.speech p").outerWidth();//段落宽度
          var $switcher = $(this).parent();//框对象
          var switcherWidth = $switcher.outerWidth();//框的宽度
          $switcher.animate({borderWidth:"5px",height:"+=20px",left:paraWidth-switcherWidth},3000);
        });*/
        //需求12

        /*$("div.label").click(function(e) {//同步
          var paraWidth = $("div.speech p").outerWidth();
          var $switcher = $(this).parent();
          var switcherWidth = $switcher.outerWidth();
          $switcher.css("position","relative")
                  .animate({left:paraWidth-switcherWidth},"slow")
                  .animate({height:"+=20px"},"slow")
                  .animate({borderWidth:"5px"},"slow");
        });*/

        //需求13
        /*$("div.label").click(function(e) {
          var paraWidth = $("div.speech p").outerWidth();
          var $switcher = $(this).parent();
          var switcherWidth = $switcher.outerWidth();
          $switcher.css("position","relative")
                  .fadeTo("fast",0.5)
                  .animate({left:paraWidth-switcherWidth},2000)
                  .fadeTo("slow",1.0)
                  .slideUp("slow")
                  .slideDown("slow");
        });*/

        //需求14
       /* $("div.label").click(function(e) {
          var paraWidth = $("div.speech p").outerWidth();
          var $switcher = $(this).parent();
          var switcherWidth = $switcher.outerWidth();
          $switcher.css("position","relative")
                  .fadeTo("fast",0.5)
                  //duration:动画的时间，queue与其他动画同时执行
                  .animate({left:paraWidth-switcherWidth},{duration:2000,queue:false})
                  .fadeTo("slow",1.0)
                  .slideUp("slow")
                  .slideDown("slow");
        });*/

        //需求15
        $("div.label").click(function(e) {
          var paraWidth = $("div.speech p").outerWidth();
          var $switcher = $(this).parent();
          var switcherWidth = $switcher.outerWidth();
          $switcher.css("position","relative")
                  .fadeTo("fast",0.5)
                  //duration动画的时间，queue与其他动画同时执行
                  .animate({left:paraWidth-switcherWidth},{duration:2000,queue:false})
                  .fadeTo("slow",1.0)
                  .slideUp("slow")
                  .queue(function(next){
                    //$switcher.css({"background-color":"#F00"});
                    $('p').eq(1).slideUp("fast").slideDown("fast")
                    next();		//与后续链接在一起
                  })
                  .slideDown("slow");
        })

        //需求16
        $("p").eq(2).css("border","1px solid #333").click(function(e) {
          $(this).slideUp("slow").next().slideDown("slow");
        });
        $("p").eq(3).css("background-color","#ccc").hide();


        //需求17
       /* $("p").eq(2).css("border","1px solid #333").click(function(e) {
          var $clickedItem = $(this);
          //slideDown 先执行，回调后执行
          $clickedItem.next().slideDown("slow",function(){
            $clickedItem.slideUp("slow");
          });
        });*/

        //需求18
        $("div.label").click(function(e) {
          //console.log($("div.speech p").outerHeight())
          var paraWidth = $("div.speech p").outerWidth();
          var $switcher = $(this).parent();
          var switcherWidth = $switcher.outerWidth();
          $switcher.css("position","relative")
                  .fadeTo("fast",0.5)
                  //duration动画的时间，queue与其他动画同时执行
                  .animate({left:paraWidth-switcherWidth},{duration:2000,queue:false})
                  .fadeTo("slow",1.0)  //速度和透明度，第三个参数为执行函数
                  .slideUp("slow",function(){
                    $switcher.css("background-color","#f00");
                  })
                  .slideDown("slow");
        });

      })
    </script>
  </head>
  <body>
    <div id="container">
      <h2>Abraham Lincoln's Gettysburg Address</h2>
      <div id="switcher">
        <div class="label">Text Size</div>
        <button id="switcher-default">Default</button>
        <button id="switcher-large">Bigger</button>
        <button id="switcher-small">Smaller</button>
      </div>
      <div class="speech">
        <p>Fourscore and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal.</p>
        <p>Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battlefield of that war. We have come to dedicate a portion of that field as a final resting-place for those who here gave their lives that the nation might live. It is altogether fitting and proper that we should do this. But, in a larger sense, we cannot dedicate, we cannot consecrate, we cannot hallow, this ground.</p>
        <a href="#" class="more">read more</a>
        <p>The brave men, living and dead, who struggled here have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember, what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced.</p>
        <p>It is rather for us to be here dedicated to the great task remaining before us&mdash;that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion&mdash;that we here highly resolve that these dead shall not have died in vain&mdash;that this nation, under God, shall have a new birth of freedom and that government of the people, by the people, for the people, shall not perish from the earth.</p>
      </div>
    </div>
  </body>
</html>
